package com.practice.httputils.controller;

import io.minio.MinioClient;
import org.apache.commons.io.IOUtils;
import org.junit.Test;

import java.io.InputStream;
import java.util.List;

/**
 * @Description : minio test
 * @Version : V1.0.0
 * @Date : 2023/1/12 16:31
 */
public class MinioTest {

    private String endpoint = "http://172.16.6.45:9000";
    private String accessKey = "minioadmin";
    private String secretKey = "minioadmin";

    @Test
    public void contextLoads() throws Exception{
        // 1.使用MinIo服务的URL，端口 账号和密码 创建一个 MinIoClient对象
        MinioClient minioClient = new MinioClient(endpoint, accessKey, secretKey);
        String bucketName = "test2";
        boolean isExists = minioClient.bucketExists(bucketName);
        if(isExists){
            System.out.println("已经存在了 test 这个 Bucket");
        }else{
            minioClient.makeBucket(bucketName);
        }
        // 存储文件到 存储桶中
        minioClient.putObject(bucketName,"/group1/UserMapper.xml","D:\\workspace\\practice\\study\\httpUtils\\pom.xml",null);
        System.out.println("文件上传成功...");
        // 下载文件
        InputStream in = minioClient.getObject(bucketName, "/group1/UserMapper.xml");
        List<String> strings = IOUtils.readLines(in, "UTF-8");
        strings.stream().forEach(s -> System.out.println(s));
    }
}
